#!/bin/bash

# Backup script for Hysteria data

BACKUP_DIR="/backup/hysteria"
DATA_DIR="/var/lib/hysteria"
CONFIG_DIR="/etc/hysteria"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

echo "Creating backup..."

# Create backup directory
mkdir -p "$BACKUP_DIR"

# Backup database
if [ -f "$DATA_DIR/admin.db" ]; then
    echo "Backing up database..."
    cp "$DATA_DIR/admin.db" "$BACKUP_DIR/admin_$TIMESTAMP.db"
fi

# Backup configuration
if [ -d "$CONFIG_DIR" ]; then
    echo "Backing up configuration..."
    tar -czf "$BACKUP_DIR/config_$TIMESTAMP.tar.gz" -C "$CONFIG_DIR" .
fi

# Remove old backups (keep last 7 days)
find "$BACKUP_DIR" -name "*.db" -mtime +7 -delete
find "$BACKUP_DIR" -name "*.tar.gz" -mtime +7 -delete

echo "Backup completed: $BACKUP_DIR"
echo "Database: admin_$TIMESTAMP.db"
echo "Config: config_$TIMESTAMP.tar.gz"
